//
//  JAVisitorView.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/2.
//  Copyright © 2017年 Arvin. All rights reserved.
/*
 自定义子视图的步骤
 1. 创建一个子视图的文件, 让它继承UIView
 2. 重写init方法
 3. 创建setupUI的方法, 并在init方法调用
 4. 创建子控件的属性
 5. 在setupUI的方法中, 添加控件, 设置控件, 自动布局
 */


import UIKit

///创建一个点击的代理
protocol JAVisitorViewDelegate: NSObjectProtocol {
    
    func login()
    
}

class JAVisitorView: UIView {
    
    ///定义一个代理属性
    weak var delegate: JAVisitorViewDelegate?

    ///大图标
    lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    ///大菊花
    lazy var circleImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    ///遮罩视图
     lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    ///文字label
    lazy var titleLable: UILabel = UILabel(title: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知",alignment: .center)
    
    ///注册按钮
    lazy var registButton: UIButton = UIButton(title: "注册", titleColor: UIColor.orange, bgImage: "common_button_white_disable", target: self, action: #selector(buttonClick))
    
    
    ///登录按钮
     lazy var loginButton: UIButton = UIButton(title: "登录",titleColor: UIColor.orange, bgImage: "common_button_white_disable", target: self, action: #selector(buttonClick))
    
    ///图标和文本信息的字典
    var visitorInfo: [String: Any]? {
        didSet {
            
            iconImageView.image = UIImage(named:visitorInfo?["imageName"] as! String)
            
            titleLable.text = visitorInfo?["message"] as? String
            
            //判断菊花是否需要转动
            if let _ = visitorInfo?["isAnimation"] as? Bool {
                circleImageView.isHidden = false
            }else {
                circleImageView.isHidden = true
            }
        }
        
        
    }
    
    
    ///构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//MARK: - 事件处理
extension JAVisitorView {
   @objc fileprivate func buttonClick() {

        delegate?.login()
    }
}

// MARK: - 设置UI
extension JAVisitorView {
    fileprivate func setupUI() {
        
        self.backgroundColor = UIColor.rgbColor(r: 237, g: 237, b: 237)
        
        //1.添加控件
        addSubview(iconImageView)
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(titleLable)
        addSubview(registButton)
        addSubview(loginButton)
    
    
        //2.自动布局
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
        }
        
        circleImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.centerY.equalTo(iconImageView)
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.centerY.equalTo(iconImageView).offset(40)
            make.left.right.equalTo(self)
        }
        
        titleLable.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.centerY.equalTo(iconImageView).offset(150)
            make.left.equalTo(self).offset(50)
            make.right.equalTo(self).offset(-50)
        }
        
        registButton.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(70)
            make.bottom.equalTo(self).offset(-120)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-70)
            make.bottom.equalTo(registButton)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        //添加动画
        addAnimation()
        
    }
    
    ///动画
    fileprivate func addAnimation() {
        //设置旋转动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI //360度旋转
        animation.duration = 15 //动画执行的时长
        animation.repeatCount = MAXFLOAT //设置动画执行的次数无限大
        animation.isRemovedOnCompletion = false //当切换页面之后, 再回来仍然可以转动
        
        circleImageView.layer.add(animation, forKey: nil)
        
    }
}
